package com.java2007.hotel.controller.front;

import com.alibaba.druid.util.StringUtils;
import com.java2007.hotel.controller.backend.BaseServlet;
import com.java2007.hotel.entity.PageBean;
import com.java2007.hotel.factory.BeanFactory;
import com.java2007.hotel.pojo.DinnerTable;
import com.java2007.hotel.pojo.Food;
import com.java2007.hotel.pojo.FoodType;
import com.java2007.hotel.service.DinnerTableService;
import com.java2007.hotel.service.FoodService;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author ghy
 * @version 1.0
 * @date 2021-01-25
 */
@WebServlet("/front")
public class FrontController extends BaseServlet {

    private DinnerTableService dinnerTableService = (DinnerTableService)BeanFactory.getBean("com.java2007.hotel.service.impl.DinnerTableServiceImpl");
    private FoodService foodService = (FoodService) BeanFactory.getBean("com.java2007.hotel.service.impl.FoodServiceImpl");

    /**
     * 首页展示空闲的餐桌
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    //findTableByStatus
    public String findTableByStatus(HttpServletRequest request, HttpServletResponse response) throws Exception {

        List<DinnerTable> tables = dinnerTableService.findByStatus(0);
        request.setAttribute("tables", tables);

        return "forward:/front/index.jsp";

    }

    /**
     * 菜单页面展示分页菜品数据
     *  分页显示 6 条
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String findFoodByPage(HttpServletRequest request, HttpServletResponse response) throws Exception {

        String pageNoStr = request.getParameter("pageNo");
        if (StringUtils.isEmpty(pageNoStr)) {
            pageNoStr = "1";
        }
        int pageNo = Integer.parseInt(pageNoStr);
        PageBean<Food> pageBean = foodService.findByPage(pageNo, PageBean.SIZE);
        request.setAttribute("pageBean", pageBean);
        return "forward:/front/detail/caidan.jsp";

    }
}
